package com.cqnu.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cqnu.common.Constant;
import com.cqnu.entity.Major;
import com.cqnu.entity.Student;
import com.cqnu.entity.SuperUser;
import com.cqnu.entity.Teacher;
import com.cqnu.entity.Thesis;
import com.cqnu.entity.commonModel.AjaxResult;
import com.cqnu.entity.commonModel.PageParameters;
import com.cqnu.util.DateUtils;
import com.cqnu.util.MD5Util;
import com.cqnu.util.StringUtil;

/**
 * 
 */
@Controller
@RequestMapping(value = "/superuser")
public class SuperUserController extends BaseController {

	/**
	 * 学生管理
	 */
	@RequestMapping("/goStuManage.do")
	public String goStuManage(HttpServletRequest request, ModelMap modelMap,
			Model model) {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}

		Map<String, Object> map = new HashMap<String, Object>();
		model.addAttribute("majorList", JSON.toJSON(majorService.find(map)));
		return "superUserManage/stu";
	}

	@RequestMapping("/stuData.do")
	@ResponseBody
	public AjaxResult stuData(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap,
			PageParameters par, Student stu) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		Map<String, Object> map = this.getMap(par);
		if (stu != null) {
			if (StringUtil.notEmpty(stu.getName())) {
				map.put("name", "%" + stu.getName() + "%");
			}
			if (stu.getNumber() != null) {
				map.put("number", stu.getNumber());
			}
		}
		aj.setRows(studentService.find(map));
		aj.setTotal(studentService.getTotal(this.clearPar(map)).intValue());
		int tal = paperLogService.getTotal(this.clearPar(map)).intValue();
		System.out.println("------------------"+tal);
		return aj;
	}

	@RequestMapping("/del.do")
	@ResponseBody
	public AjaxResult del(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (null != studentService.getById(id)) {
			if (studentService.delete(id) == 1) {
				aj.setSuccess(true);
				aj.setMessage("删除成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("删除失败！");
		return aj;
	}

	@RequestMapping("/addOrUpdate.do")
	@ResponseBody
	public AjaxResult addOrUpdate(HttpServletRequest request,
			HttpServletResponse response, Student stu) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (stu.getId() != null) {
			if (null != studentService.getById(stu.getId())) {
				if (studentService.update(stu) == 1) {
					aj.setSuccess(true);
					aj.setMessage("修改成功！");
					return aj;
				}
			}
		} else {
			stu.setPassword(MD5Util.MD5(Constant.INITPEWD));
			int rs = studentService.add(stu);
			if (rs == 1) {
				aj.setSuccess(true);
				aj.setMessage("添加成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("操作失败！");
		return aj;
	}

	@RequestMapping("/getStuById.do")
	@ResponseBody
	public AjaxResult getStuById(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		aj.setSuccess(true);
		aj.setData(studentService.getById(id));
		return aj;
	}

	// --------老师------

	@RequestMapping("/goTeaManage.do")
	public String goTeaManage(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap, Model model) {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}

		Map<String, Object> map = new HashMap<String, Object>();
		model.addAttribute("majorList", JSON.toJSON(majorService.find(map)));
		return "superUserManage/tea";
	}

	@RequestMapping("/teaData.do")
	@ResponseBody
	public AjaxResult teaData(HttpServletRequest request,
			HttpServletResponse response, PageParameters par, Teacher tea)
			throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		Map<String, Object> map = this.getMap(par);
		if (tea != null) {
			if (StringUtil.notEmpty(tea.getName())) {
				map.put("name", "%" + tea.getName() + "%");
			}
			if (tea.getNumber() != null) {
				map.put("number", tea.getNumber());
			}
		}
		aj.setRows(teacherService.find(map));
		aj.setTotal(teacherService.getTotal(this.clearPar(map)).intValue());
		return aj;
	}

	@RequestMapping("/deltea.do")
	@ResponseBody
	public AjaxResult deltea(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (null != teacherService.getById(id)) {
			if (teacherService.delete(id) == 1) {
				aj.setSuccess(true);
				aj.setMessage("删除成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("删除失败！");
		return aj;
	}

	@RequestMapping("/teaaddOrUpdate.do")
	@ResponseBody
	public AjaxResult teaaddOrUpdate(HttpServletRequest request,
			HttpServletResponse response, Teacher tea) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (tea.getId() != null) {
			if (null != teacherService.getById(tea.getId())) {
				if (teacherService.update(tea) == 1) {
					aj.setSuccess(true);
					aj.setMessage("修改成功！");
					return aj;
				}
			}
		} else {
			tea.setPassword(MD5Util.MD5(Constant.INITPEWD));
			if (teacherService.add(tea) == 1) {
				aj.setSuccess(true);
				aj.setMessage("添加成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("修改失败！");
		return aj;
	}

	@RequestMapping("/getTeaById.do")
	@ResponseBody
	public AjaxResult getTeaById(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath()
					+ "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		aj.setSuccess(true);
		aj.setData(teacherService.getById(id));
		return aj;
	}

	/**
	 * 课题管理
	 */
	@RequestMapping("/goThesisManage.do")
	public String goThesisManage(HttpServletRequest request, ModelMap modelMap,
			Model model) {

		SuperUser sessionInfo = (SuperUser) this.getSession(request,
				Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request,
				Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您还未登录或身份已过期，请登录后进行操作！");
			return "error";
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			// Message报出
			modelMap.addAttribute("errorMessage", "您没有该资源操作权限！");
			return "error";
		}

		Map<String, Object> map = new HashMap<String, Object>();
		model.addAttribute("majorList", JSON.toJSON(majorService.find(map)));
		return "superUserManage/thesis";
	}

	@RequestMapping("/thesisData.do")
	@ResponseBody
	public AjaxResult thesisData(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap,
			PageParameters par, Thesis thesis) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request, Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath() + "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath() + "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		Map<String, Object> map = this.getMap(par);
		if ( thesis != null ) {
			if ( thesis.getMajorId() != null ) {
				map.put("majorId", thesis.getMajorId());
			}
			if ( StringUtil.notEmpty(thesis.getThesisName()) ) {
				map.put("thesisName", "%" + thesis.getThesisName() + "%");
			}
			if ( thesis.getStatus() != null ) {
				map.put("status", "%" + thesis.getStatus() + "%");
			}
		}
		aj.setRows(thesisService.find(map));
		aj.setTotal(thesisService.getTotal(this.clearPar(map)).intValue());
		return aj;
	}

	@RequestMapping("/delThesis.do")
	@ResponseBody
	public AjaxResult delThesis(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request, Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath() + "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath() + "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (null != thesisService.getById(id)) {
			if (thesisService.delete(id) == 1) {
				aj.setSuccess(true);
				aj.setMessage("删除成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("删除失败！");
		return aj;
	}

	@RequestMapping("/addOrUpdateThesis.do")
	@ResponseBody
	public AjaxResult addOrUpdateThesis(HttpServletRequest request,
			HttpServletResponse response, Thesis thesis) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request, Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath() + "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath() + "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		if (thesis.getId() != null) {
			if (null != thesisService.getById(thesis.getId())) {
				if (thesisService.update(thesis) == 1) {
					aj.setSuccess(true);
					aj.setMessage("修改成功！");
					return aj;
				}
			}
		} else {
			thesis.setCreattime(DateUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
			int rs = thesisService.add(thesis);
			if (rs == 1) {
				aj.setSuccess(true);
				aj.setMessage("添加成功！");
				return aj;
			}
		}
		aj.setSuccess(false);
		aj.setMessage("操作失败！");
		return aj;
	}

	@RequestMapping("/getThesisById.do")
	@ResponseBody
	public AjaxResult getThesisById(HttpServletRequest request,
			HttpServletResponse response, int id) throws Exception {

		SuperUser sessionInfo = (SuperUser) this.getSession(request, Constant.SESSION_USER_KEY);
		String sessionRole = (String) this.getSession(request, Constant.SESSION_ROLE_KEY);

		// 用户未登录--返回登录页
		if (sessionInfo == null) {
			response.sendRedirect(request.getContextPath() + "/home/errorInfo.do");
			return null;
		}
		// 用户角色错误--返回登录页
		if (!Constant.LOGIN_TYPE_ADMIN.equals(sessionRole)) {
			response.sendRedirect(request.getContextPath() + "/home/errorRole.do");
			return null;
		}

		AjaxResult aj = new AjaxResult();
		aj.setSuccess(true);
		aj.setData(thesisService.getById(id));
		return aj;
	}
	
	
	
	/**
	 * 获取专业下拉框数据
	 */
	@RequestMapping("/getMajorInfo.do")
	@ResponseBody
	public List<Major> getMajorInfo(HttpServletRequest request, ModelMap modelMap) {
		Map<String, Object> map = new HashMap<String, Object>();
		return majorService.find(map);
	}
}
